const app = getApp();
var funcs = require('../../../static/js/funcs.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dealStateList:['是','否'],
    dbList:["无","保证","抵押","质押","保证+抵押","保证+质押"],
    riskList:["正常","关注","次级","可疑","损失"],
    moreTogether:[
      {
          "bName":"",
          "bPhone":"",
          "bIdcard":""
      }
    ], 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    console.log("新增不良贷款页面")
    let openid = app.globalData.openid
    funcs.judgeStaff(openid).then(res=>{
      if(res.data.succeed){
        console.log(res.data.data)
        console.log("该员工已注册")
        that.setData({
          staffName:res.data.data.name,
          staffOanum: res.data.data.oanum,
          staffPhone: res.data.data.telnum
        })
      }else{
        app.globalData.loginedFlag = false
        wx.reLaunch({
          url: '/pages/my/my',
        })
        wx.showToast({
          title: "请先绑定",
          icon: 'error',
          duration: 2000
        })
      }
    })
  },


  select_date_func: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    // let result = e.detail.value.split('-');
    // console.log(result)
    this.setData({
      jkDate:e.detail.value
    })
  },  

  select_date_func2(e){
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      dqDate:e.detail.value
    })
  },
  bindPickerChangeDeal(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      isPledged: this.data.dealStateList[e.detail.value]
    })
  },
  bindPickerChangeDb(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      wayGuarantee: this.data.dbList[e.detail.value]
    })
  },
  bindPickerChangeRisk(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      riskStatus: this.data.riskList[e.detail.value]
    })
  },
  clickAdd(){
    let currarr = this.data.moreTogether
    let map ={
      "bName":"",
      "bPhone":"",
      "bIdcard":""
    }
    currarr.push(map)
    this.setData({
      moreTogether:currarr
    })
    console.log(this.data.moreTogether)
  },
  clickDelete(){
    let currarr = this.data.moreTogether
    if(currarr.length==1){
      console.log("已经是最后一个了，无法删除")
      return
    }
    currarr.pop()
    this.setData({
      moreTogether:currarr
    })
  },

  /**
   * 共同借款人相关
   */
  tinputName(e){
    console.log(e)
    let id = e.currentTarget.dataset.id
    let bName = e.detail.value
    let tmpTogether = this.data.moreTogether
    tmpTogether[id].bName = bName
    this.setData({
      moreTogether:tmpTogether
    })
  },
  tinputphone(e){
    console.log(e)
    let id = e.currentTarget.dataset.id
    let bPhone = e.detail.value
    let tmpTogether = this.data.moreTogether
    tmpTogether[id].bPhone = bPhone
    this.setData({
      moreTogether:tmpTogether
    })
  },
  tinputidcard(e){
    console.log(e)
    let id = e.currentTarget.dataset.id
    let bIdcard = e.detail.value
    let tmpTogether = this.data.moreTogether
    tmpTogether[id].bIdcard = bIdcard
    this.setData({
      moreTogether:tmpTogether
    })
  },
    //表单信息校验
  todoVerify(){
    //必填信息过滤
    if(this.data.uname==null||this.data.uidcard==null||this.data.jkDate==null||this.data.dqDate==null||this.data.jkAmount==null||this.data.tqAmount==null||this.data.tqAmountInterest==null||this.data.wayGuarantee==null||this.data.riskStatus==null||this.data.loanStatus==null||this.data.personGuarantee==null||this.data.personGuaranteePhone==null||this.data.personGuaranteeIdcard==null){
      wx.showToast({
        title: '请补充必填项',
        icon: "error",
        duration: 3000
      })
      return false
    }

    if(this.data.staffPhone!=null){
      if(!this.vertifyPhone(this.data.staffPhone)||!this.vertifyPhone(this.data.personGuaranteePhone)){
        return false;  //员工手机号格式不对
      }
    }
    if(this.data.uname==null||!this.validateName(this.data.uname)){
      wx.showToast({
        title: '借款人格式异常',
        icon: "error",
        duration: 2000
      })
      return false;
    }
    if(this.data.personGuarantee==null||!this.validateName(this.data.personGuarantee)){
      wx.showToast({
        title: '保证人格式异常',
        icon: "error",
        duration: 2000
      })
      return false;
    }
    if(this.data.pledger==null||!this.validateName(this.data.pledger)){
      wx.showToast({
        title: '抵押人格式异常',
        icon: "error",
        duration: 2000
      })
      return false;
    }
    if(this.data.uidcard==null||!this.isValidID(this.data.uidcard)){
      wx.showToast({
        title: '借款人身份信息错误',
        icon: "none",
        duration: 2000
      })
      return false;
    }
    if(this.data.personGuaranteeIdcard==null||!this.isValidID(this.data.personGuaranteeIdcard)){
      wx.showToast({
        title: '保证人身份信息错误',
        icon: "none",
        duration: 2000
      })
      return false;
    }
    if(this.data.pledgerIdcard==null||!this.isValidID(this.data.pledgerIdcard)){
      wx.showToast({
        title: '抵押人身份信息错误',
        icon: "none",
        duration: 2000
      })
      return false;
    }
    if(!this.isNumeric(this.data.jkAmount)||!this.isNumeric(this.data.tqAmount)||!this.isNumeric(this.data.tqAmountInterest)||!this.isNumeric(this.data.pgValue)||!this.isNumeric(this.data.czValue)){
      wx.showToast({ 
        title: '金额格式异常',
        icon: "error",
        duration: 2000
      })
      return false;
    }

  },

  submit(e){
    let that = this
    wx.showModal({
      title: '信息核对',
      content: '是否提交录入？',
      success(res) {
        if (res.confirm) {
          let openid = app.globalData.openid

          //校验输入
          if(that.todoVerify()==false)
            return;
          //封装输入
          let preLgInfo = {
            openid:openid,
            staffName: that.data.staffName,
            staffOanum: that.data.staffOanum,
            staffInstNum:that.data.staffInstNum==null? "":that.data.staffInstNum,
            staffPhone:that.data.staffPhone==null? "":that.data.staffPhone,
            staffInstName:that.data.staffInstName==null? "":that.data.staffInstName,
            staffSubInstName:that.data.staffSubInstName==null? "":that.data.staffSubInstName,
            uname:that.data.uname==null? "":that.data.uname,
            uidcard:that.data.uidcard==null? "":that.data.uidcard,
            jkDate:that.data.jkDate==null? "":that.data.jkDate,
            dqDate:that.data.dqDate==null? "":that.data.dqDate,
            jkAmount:that.data.jkAmount==null? "":that.data.jkAmount,
            tqAmount:that.data.tqAmount==null? "":that.data.tqAmount,
            tqAmountInterest:that.data.tqAmountInterest==null? "":that.data.tqAmountInterest,
            wayGuarantee:that.data.wayGuarantee==null? "":that.data.wayGuarantee,
            riskStatus:that.data.riskStatus==null? "":that.data.riskStatus,
            loanStatus:that.data.loanStatus==null? "":that.data.loanStatus,
            personGuarantee:that.data.personGuarantee==null? "":that.data.personGuarantee,
            personGuaranteePhone:that.data.personGuaranteePhone==null? "":that.data.personGuaranteePhone,
            personGuaranteeIdcard:that.data.personGuaranteeIdcard==null? "":that.data.personGuaranteeIdcard,
            pledger:that.data.pledger==null? "":that.data.pledger,
            pledgerIdcard:that.data.pledgerIdcard==null? "":that.data.pledgerIdcard,
            pgValue:that.data.pgValue==null? "":that.data.pgValue,
            czValue:that.data.czValue==null? "":that.data.czValue,
            pledgedProperty:that.data.pledgedProperty==null? "":that.data.pledgedProperty,
            pledgedAddress:that.data.pledgedAddress==null? "":that.data.pledgedAddress,
            isPledged:that.data.isPledged==null? "":that.data.isPledged,
            pledgedWay:that.data.pledgedWay==null? "":that.data.pledgedWay,
            status:"正常",
            borrowers:that.data.moreTogether
          }
          console.log(preLgInfo)
          funcs.submitLg(preLgInfo).then(res=>{
            if(res.data.succeed){
              wx.navigateTo({
                url: '/pages/ss/ssindex/ssindex',
              })
              wx.showToast({
                title: '提交成功',
                icon: "success",
                duration: 2000
              })
            }else{
              return
            }
          })
        }
      }
    })
  },
  instNumInput(e){
    console.log("staffInstNum>",e.detail.value)
    this.setData({
      staffInstNum:e.detail.value
    })
  },
  stafftelinput(e){
    console.log("staffPhone>",e.detail.value)
    this.setData({
      staffPhone:e.detail.value
    })
  },
  instInput(e){
    console.log("staffInstName>",e.detail.value)
    this.setData({
      staffInstName:e.detail.value
    })
  },
  instInputSub(e){
    console.log("staffSubInstName>",e.detail.value)
    this.setData({
      staffSubInstName:e.detail.value
    })
  },
  //必填选项
  inputBuserName(e){
    console.log("uname>",e.detail.value)
    this.setData({
      uname:e.detail.value
    })
  },
  inputBuserIdcard(e){
    console.log("uidcard>",e.detail.value)
    this.setData({
      uidcard:e.detail.value
    })
  },
  inputjkAmount(e){
    console.log("jkAmount>",e.detail.value)
    this.setData({
      jkAmount:e.detail.value
    })
  },
  inputtqAmount(e){
    console.log("tqAmount>",e.detail.value)
    this.setData({
      tqAmount:e.detail.value
    })
  },
  inputtqAmountInterest(e){
    console.log("tqAmountInterest>",e.detail.value)
    this.setData({
      tqAmountInterest:e.detail.value
    })
  },
  inputloanStatus(e){
    console.log("loanStatus>",e.detail.value)
    this.setData({
      loanStatus:e.detail.value
    })
  },
  
  inputpersonGuarantee(e){
    console.log("personGuarantee>",e.detail.value)
    this.setData({
      personGuarantee:e.detail.value
    })
  },
  inputpersonGuaranteePhone(e){
    console.log("personGuaranteePhone>",e.detail.value)
    this.setData({
      personGuaranteePhone:e.detail.value
    })
  },
  inputpersonGuaranteeIdcard(e){
    console.log("personGuaranteeIdcard>",e.detail.value)
    this.setData({
      personGuaranteeIdcard:e.detail.value
    })
  },
  inputpledger(e){
    console.log("pledger>",e.detail.value)
    this.setData({
      pledger:e.detail.value
    })
  },

  inputpledgerIdcard(e){
    console.log("pledgerIdcard>",e.detail.value)
    this.setData({
      pledgerIdcard:e.detail.value
    })
  },
  inputpgValue(e){
    console.log("pgValue>",e.detail.value)
    this.setData({
      pgValue:e.detail.value
    })
  },
  inputczValue(e){
    console.log("czValue>",e.detail.value)
    this.setData({
      czValue:e.detail.value
    })
  },
  inputpledgedProperty(e){
    console.log("pledgedProperty>",e.detail.value)
    this.setData({
      pledgedProperty:e.detail.value
    })
  },
  inputpledgedAddress(e){
    console.log("pledgedAddress>",e.detail.value)
    this.setData({
      pledgedAddress:e.detail.value
    })
  },
  inputpledgedWay(e){
    console.log("pledgedWay>",e.detail.value)
    this.setData({
      pledgedWay:e.detail.value
    })
  },
  
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 手机号格式验证
  vertifyPhone: function (phone) {
    if (!/^1[3456789]\d{9}$/.test(phone)) {
      // 手机号格式不正确，提示用户
      wx.showToast({
        title: '手机格式错误',
        icon: 'error'
      });
      return false;
    }
    return true;
  },
  isValidID:function(id) {
    if (id.length !== 18) {
      return false;
    }
    const weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    const checkCode = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    let sum = 0;
    for (let i = 0; i < 17; i++) {
      sum += parseInt(id[i]) * weight[i];
    }
    const index = sum % 11;
    if (checkCode[index] !== id[17].toUpperCase()) {
      return false;
    }
    return true;
  },
  validateName:function (name) {
    // const regex = /^[a-zA-Z\u4e00-\u9fa5]+( [a-zA-Z\u4e00-\u9fa5]+)*$/;
    // return regex.test(name);
    // 收货人姓名校验(准则:姓名为2-8汉字)

      var regu = /^[\u4E00-\u9FA5]{2,8}$/;
      var re = new RegExp(regu);
      if (!re.test(name)) {
        return false;
      }
      return true;
  },
  isNumeric(str) {
    if(str==null)
      return false
    return !isNaN(Number(str));
  },
})